#!/bin/bash

# configure
# Configuration and installation script for
# the tovid suite, version 0.18
# ==============
# Project homepage: http://tovid.sourceforge.net/
#
# This software is licensed under the GNU General Public License
# For the full text of the GNU GPL, see:
#
#   http://www.gnu.org/copyleft/gpl.html
#
# No guarantees of any kind are associated with use of this software.

WELCOME=`cat << 'EOF'
-----------------------------
tovid suite configurator and installer
Version 0.18
Written in 2004-2005 by Eric Pierce
http://tovid.sourceforge.net/
-----------------------------

You are running the configuration and installation script for
the tovid suite. This script will check your computer for
necessary and optional components, and can install the suite into
a system-wide directory of your choosing. This script may also
be used to remove the tovid suite, if you decide you do not
want it to be installed any longer.
EOF`

GOODBYE=`cat << 'EOF'
Please check out the tovid documentation on the web:
  http://tovid.sourceforge.net/
Questions? Comments? Visit the tovid discussion forum:
  http://tovid.dk3.com/
EOF`

# List of suite components, with description
TOVID="tovid (Converts video to (S)VCD or DVD mpeg format)"
IDVID="idvid (Identifies video format, resolution, and length)"
MAKEMENU="makemenu (Creates (S)VCD/DVD menus)"
MAKEXML="makexml (Creates XML specification for an (S)VCD or DVD navigation hierarchy)"
POSTPROC="postproc (Adjusts A/V sync and does shrinking of encoded video)"
DVREQUANT="dvrequant (Shrinks and re-authors titles from existing DVDs)"
# Default location for components to be installed to
DEFAULT_PATH="/usr/local/bin"

# List of components to install or remove
COMPONENTS="tovid idvid makemenu makexml postproc \
tovid-batch tovid-interactive \
makevcd makedvd tovid-test"

# Check for required programs and utilities
MPLAYER=`type -p mplayer`
MENCODER=`type -p mencoder`
YUVFPS=`type -p yuvfps`
MPEG2ENC=`type -p mpeg2enc`
MP2ENC=`type -p mp2enc`
MPLEX=`type -p mplex`
NORMALIZE=`type -p normalize`
# Try newer version of normalize (normalize-audio)
if [[ -z $NORMALIZE ]]; then
  NORMALIZE=`type -p normalize-audio`
fi
IMCONVERT=`type -p convert`
FFMPEG=`type -p ffmpeg`
DVDAUTHOR=`type -p dvdauthor`
TRANSCODE=`type -p transcode`
VCDIMAGER=`type -p vcdimager`
SOX=`type -p sox`
CDRECORD=`type -p mkisofs`
LSDVD=`type -p lsdvd`
UNMET_REQS="n"
CHECK_ONLY="n"
# See what UNIX flavor is in use
UNIX_FLAVOR=$( uname -s )

# See if libdvdcss is available
if [[ $UNIX_FLAVOR == "Linux" ]]; then
    LIBDVDCSS=$( /sbin/ldconfig -p | grep 'libdvdcss' )
else
    LIBDVDCSS=$( /sbin/ldconfig -r | grep 'libdvdcss' )
fi

SEPARATOR="=========================================="

# *********************************
# EXECUTION BEGINS HERE
# *********************************

echo $"$WELCOME"
echo $SEPARATOR
echo "Do you want to [I]nstall, [R]emove, or [C]heck only (I, R, or C)?"
read INST_ACTION

if [[ $INST_ACTION == "I" ]] || [[ $INST_ACTION == "i" ]]; then
    echo $SEPARATOR
    echo "Installing the tovid suite"
    echo $SEPARATOR
    echo "Where would you like to install the suite?"
    echo "(Just press enter to use the default: $DEFAULT_PATH)"
    read INST_PATH
    # Use default path if none specified
    [[ -z $INST_PATH ]] && INST_PATH=$DEFAULT_PATH
    echo $SEPARATOR
    # -------------------
    # The rest of the installation proceeds below
    # (after the elif and else portions)
    # -------------------
elif [[ $INST_ACTION == "R" ]] || [[ $INST_ACTION == "r" ]]; then
    echo $SEPARATOR
    echo "Removing the tovid suite"
    echo $SEPARATOR
    echo "Where is the tovid suite installed (default: $DEFAULT_PATH)?"
    read INST_PATH
    # Use default path if none specified
    [[ -z $INST_PATH ]] && INST_PATH=$DEFAULT_PATH

    # If user is not owner of installed files, prompt for root access
    if [[ ! -O "$INST_PATH/tovid" ]]; then
        echo "Uninstalling from $INST_PATH requires root permission. At the"
        echo "prompt, please enter the root password."
        ERROR=$( su -c "cd $INST_PATH && rm -f $COMPONENTS" )
    # Otherwise, just remove them
    else
        ERROR=$( cd $INST_PATH && rm -f $COMPONENTS )
    fi

    if [ ! $ERROR ]; then
        echo "The following files were removed from $INST_PATH:"
        echo "  $COMPONENTS"
        echo "Thanks for trying tovid!"
        exit 0
    else
        echo "The following files could not be successfully removed from $INST_PATH:"
        echo "  $COMPONENTS"
        echo "Perhaps you specified the wrong path name. If you forgot where you"
        echo "installed tovid, you can locate it by running the following command:"
        echo "  find . -name tovid"
        echo "If you are unable to get this script to uninstall tovid, you can manually"
        echo "remove the files listed above from wherever they are installed."
        exit 1
    fi
else
    echo "Checking configuration only. Nothing will be installed or removed."
    echo $SEPARATOR
    CHECK_ONLY="y"
fi

# -------------------
# Remainder of installation and configuration-checking begins here
# -------------------

# Check for supported UNIX flavors
echo "UNIX flavor: $UNIX_FLAVOR"
if [[ $UNIX_FLAVOR != "Linux" ]]; then
    echo $SEPARATOR
    echo "It looks like you're using a non-Linux system (though I may"
    echo "be wrong; I'm just a dumb installer). BSD and Cygwin are"
    echo "partially supported, but tovid is primarily tested on Linux."
    echo "The developer(s) of tovid would be very happy to learn that"
    echo "tovid works on other non-Linux platforms, so please give it"
    echo "a try and report any problems you have."
    echo $SEPARATOR
fi

# Check for required versions among dependencies, where applicable

echo "Checking for required and recommended programs..."
echo $SEPARATOR

# Make sure mplayer at least supports yuv4mpeg output
if [[ -n $MPLAYER ]]; then
  MPLAYER_WORKS=$( mplayer -vo help 2>&1 | grep yuv4mpeg )
fi
echo "mplayer/mencoder: $MPLAYER"
if [[ -z $MPLAYER ]] || [[ -z $MENCODER ]] || [[ -z $MPLAYER_WORKS ]]; then
  echo $SEPARATOR
  echo "mplayer was not found, or is not new enough."
  echo "mplayer is needed for the following suite components:"
  echo "  $TOVID"
  echo "  $IDVID"
  echo "  $POSTPROC"
  echo "  $DVREQUANT"
  echo "  $MAKEXML"
  echo "If you want to use the above components, please install mplayer."
  echo "You can get mplayer from http://www.mplayerhq.hu/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

echo "mjpegtools: $YUVFPS $MPEG2ENC $MP2ENC $MPLEX"
if [[ -z $YUVFPS ]] || [[ -z $MPEG2ENC ]] ||
   [[ -z $MP2ENC ]] || [[ -z $MPLEX ]]; then
  echo $SEPARATOR
  echo "The mjpegtools package was not found, or is incomplete."
  echo "This package is needed for the following suite components:"
  echo "  $TOVID"
  echo "  $MAKEMENU"
  echo "  $DVREQUANT"
  echo "If you want to use the above components, please install mjpegtools."
  echo "You can get mjpegtools from http://mjpeg.sourceforge.net/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

echo "ffmpeg: $FFMPEG"
if [[ -z $FFMPEG ]]; then
  echo $SEPARATOR
  echo "FFmpeg was not found. This utility is required for encoding"
  echo "NTSC DVDs. If you are only encoding PAL, you don't need it."
  echo "It is used by the following components:"
  echo "  $TOVID"
  echo "  $MAKEMENU"
  echo "You can get ffmpeg from http://ffmpeg.sourceforge.net/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

echo "transcode: $TRANSCODE"
if [[ -z $TRANSCODE ]]; then
  echo $SEPARATOR
  echo "The transcode package was not found. This package is used for"
  echo "requantizing (shrinking) videos after they have been encoded."
  echo "If you don't need to do this, then you don't need transcode."
  echo "It is used by the following components:"
  echo "  $POSTPROC"
  echo "  $DVREQUANT"
  echo "You can get transcode from http://zebra.fh-weingarten.de/~transcode/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

echo "normalize: $NORMALIZE"
if [[ -z $NORMALIZE ]]; then
  echo $SEPARATOR
  echo "The normalize utility was not found. This utility is used for"
  echo "normalizing audio volume in videos; it is OPTIONAL, and used"
  echo "by the following suite components:"
  echo "  $TOVID"
  echo "You can get normalize from http://freshmeat.net/projects/normalize/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

# Make sure ImageMagick is 6.0 or newer
if [[ -n $IMCONVERT ]]; then
  IMCONVERT_WORKS=$( convert -version | grep 'ImageMagick [6789]\.' )
fi
echo "ImageMagick utilities: $IMCONVERT"
if [[ -z $IMCONVERT ]] || [[ ! $IMCONVERT ]]; then
  echo $SEPARATOR
  echo "Some needed ImageMagick utilities were not found, or are not new enough."
  echo "ImageMagick 6.0 or greater is needed for:"
  echo "  $MAKEMENU"
  echo "If you want to use the above components, please install ImageMagick."
  echo "You can get ImageMagick from http://www.imagemagick.org/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

# Check for SoX
echo "SoX: $SOX"
if [[ -z $SOX ]]; then
  echo $SEPARATOR
  echo "SoX was not found. SoX is needed for converting background audio clips"
  echo "and for creating default silent clips in (S)VCD/DVD menus. It is needed"
  echo "by the following components:"
  echo "  $MAKEMENU"
  echo "If you want to use the above components, please install SoX."
  echo "You can get SoX from http://sox.sourceforge.net/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

# Make sure dvdauthor is 0.6.0 or newer
if [[ -n $DVDAUTHOR ]]; then
  DVDAUTHOR_WORKS=$( dvdauthor 2>&1 | grep 'version 0\.[6789]\.' )
fi
echo "dvdauthor: $DVDAUTHOR"
if [[ -z $DVDAUTHOR ]] || [[ -z $DVDAUTHOR_WORKS ]]; then
  echo $SEPARATOR
  echo "dvdauthor was not found. dvdauthor is needed by the following"
  echo "components:"
  echo "  $DVREQUANT"
  echo "dvdauthor is also useful if you want to process the output of:"
  echo "  $MAKEXML"
  echo "If you want full use of the above components, please install dvdauthor."
  echo "You can get dvdauthor from http://dvdauthor.sourceforge.net/"
  echo $SEPARATOR
  UNMET_REQS="y" 
fi

# Check for vcdimager
echo "vcdimager: $VCDIMAGER"
if [[ -z $VCDIMAGER ]]; then
  echo $SEPARATOR
  echo "vcdimager was not found. vcdimager is needed by:"
  echo "It's also useful for processing the output of:"
  echo "  $MAKEXML"
  echo "If you are only interested in making DVDs (not VCDs or SVCDs), then"
  echo "you don't need vcdimager, but you might want to install it just for fun."
  echo "You can get vcdimager from http://www.vcdimager.org/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

# Check for libdvdcss
if [[ -z $LIBDVDCSS ]]; then
  echo $SEPARATOR
  echo "libdvdcss was not found. libdvdcss is used by"
  echo "the following components:"
  echo "  $DVREQUANT"
  echo "If you want to use the above components, please install libdvdcss."
  echo "You can get libdvdcss from http://developers.videolan.org/libdvdcss/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

# Check for lsdvd
if [[ -z $LSDVD ]]; then
  echo $SEPARATOR
  echo "lsdvd was not found. lsdvd is used by the following components:"
  echo "  $DVREQUANT"
  echo "If you want to use the above components, please install lsdvd."
  echo "You can get lsdvd from http://untrepid.com/acidrip/lsdvd.html"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

# Check for cdrecord/mkisofs 
if [[ -z $CDRECORD ]]; then
  echo $SEPARATOR
  echo "mkisofs (part of the cdrecord package) was not found."
  echo "mkisofs is used by the following components:"
  echo "  $DVREQUANT"
  echo "If you want to use the above components, please install cdrecord."
  echo "You can get cdrecord from http://freshmeat.net/projects/cdrecord/"
  echo $SEPARATOR
  UNMET_REQS="y"
fi

# If we're checking only, just print a message and exit
if [[ $CHECK_ONLY == "y" ]]; then
  echo $SEPARATOR
  if [[ $UNMET_REQS == "y" ]]; then
    echo "Some required or recommended software was not found. You may"
    echo "want to install these before using tovid for full functionality."
  else
    echo "You have all the required and recommended"
    echo "software for using the tovid suite!"
  fi
  echo $"$GOODBYE"
  echo $SEPARATOR
  exit 0
fi

PROCEED="y"
# If there were unmet requirements, prompt for continuation
echo $SEPARATOR
if [[ $UNMET_REQS == "y" ]]; then
  echo "Some required or recommended software was not found. You can still"
  echo "install the tovid suite, and install the other required software"
  echo "later. Do you want to proceed with installation (y/n)?"
  read PROCEED
else
  echo "Good! All of tovid's dependencies appear to be installed."
fi
echo $SEPARATOR

if [[ $PROCEED == "y" || $PROCEED == "Y" ]]; then
    # If destination is not writable, prompt for root password
    if [[ ! -w $INST_PATH ]]; then
        echo "Installing to $INST_PATH requires root permission. At the"
        echo "prompt, please enter the root password."
        ERROR=$( su -c "cp $COMPONENTS $INST_PATH" )
    # Otherwise, just copy
    else
        ERROR=$( cp $COMPONENTS "$INST_PATH" )
    fi
  echo $SEPARATOR
  if [ ! $ERROR ]; then
    echo "The following files were successfully installed to $INST_PATH:"
    echo "  $COMPONENTS"
    echo $"$GOODBYE"
  else
    echo "Installation to $INST_PATH failed for some reason. Please"
    echo "ensure that $INST_PATH exists and is writable. Sorry for the"
    echo "inconvenience!"
  fi
  echo $SEPARATOR
else
  echo "Okay, the tovid suite will not be installed at this time. Please"
  echo "try again after you've installed some of the dependencies listed above."
  echo $SEPARATOR
fi

exit 0
